<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Returns the auto generated ID of the last insert query that successfully 
  executed on this connection</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.db2-get-option.html">« db2_get_option</a></li>
      <li style="float: right;"><a href="function.db2-lob-read.html">db2_lob_read »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.ibm-db2.html">IBM DB2 函数</a></li>
    <li>Returns the auto generated ID of the last insert query that successfully 
  executed on this connection</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.db2-last-insert-id" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">db2_last_insert_id</h1>
  <p class="verinfo">(PECL ibm_db2 &gt;= 1.7.1)</p><p class="refpurpose"><span class="refname">db2_last_insert_id</span> &mdash; <span class="dc-title">Returns the auto generated ID of the last insert query that successfully 
  executed on this connection</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.db2-last-insert-id-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>db2_last_insert_id</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$resource</code></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   Returns the auto generated ID of the last insert query that successfully 
  executed on this connection.
  </p>
  <p class="para">
   The result of this function is not affected by any of the following:
   <ul class="itemizedlist">
    <li class="listitem">
     <p class="para">
      A single row INSERT statement with a VALUES clause for a table without an identity column.
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      A multiple row INSERT statement with a VALUES clause.
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      An INSERT statement with a fullselect.
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      A ROLLBACK TO SAVEPOINT statement.
     </p>
    </li>
   </ul>
  </p>   
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.db2-last-insert-id-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">resource</code></dt>

     <dd>

      <p class="para">
       A valid connection resource as returned from <span class="function"><a href="function.db2-connect.html" class="function">db2_connect()</a></span> 
       or <span class="function"><a href="function.db2-pconnect.html" class="function">db2_pconnect()</a></span>. The value of this parameter cannot be a 
       statement resource or result set resource.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.db2-last-insert-id-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the auto generated ID of last insert query that successfully 
  executed on this connection.
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.db2-last-insert-id-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="db2-last-insert-id.example.basic">
    <p><strong>示例 #1 A <span class="function"><strong>db2_last_insert_id()</strong></span> example</strong></p>
    <div class="example-contents"><p>
      The following example shows how to return the  
      auto generated ID of last insert query that successfully 
      executed on this connection.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$database&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"SAMPLE"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"db2inst1"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"ibmdb2"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$database</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br />if(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$createTable&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"CREATE&nbsp;TABLE&nbsp;lastInsertID&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(id&nbsp;integer&nbsp;GENERATED&nbsp;BY&nbsp;DEFAULT&nbsp;AS&nbsp;IDENTITY,&nbsp;name&nbsp;varchar(20))"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$insertTable&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;lastInsertID&nbsp;(name)&nbsp;VALUES&nbsp;('Temp&nbsp;Name')"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;@</span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$createTable</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Checking&nbsp;for&nbsp;single&nbsp;row&nbsp;inserted.&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$insertTable</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">=&nbsp;&nbsp;</span><span style="color: #0000BB">db2_last_insert_id</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$ret</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Last&nbsp;Insert&nbsp;ID&nbsp;is&nbsp;:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"No&nbsp;Last&nbsp;insert&nbsp;ID.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">db2_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br />}<br />else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Connection&nbsp;failed."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Last Insert ID is : 1
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div></div></div></body></html>